
/**
 * 数组去重 
 * 调用方法：dedupe(arr) 或 arr.unique
 */

function dedupe(array) {
	var arr = [];
	for(var i=0, a; a=array[i++];){
		arr.push(JSON.stringify(a));
	}
	var newArr = [];
	oldArr = Array.from(new Set(arr));
	for(var item of oldArr){
		newArr.push(JSON.parse(item));
	}
	return newArr;
}

Array.prototype.unique = function() {
	var arr = [],
		obj = {};

	for(var i = 0; i < this.length; i++) {
		if(!obj[this[i]]) {
			arr.push(this[i]);
			obj[this[i]] = 1;
		}
	}
	return arr;
}

/*
 *  方法:Array.remove(dx)
 *  功能:根据元素位置值删除数组元素.
 *  参数:元素值
 *  返回:在原数组上修改数组
 *  作者：pxp
 */
Array.prototype.remove = function (dx) {
    if (isNaN(dx) || dx > this.length) {
        return false;
    }
    for (var i = 0, n = 0; i < this.length; i++) {
        if (this[i] != this[dx]) {
            this[n++] = this[i];
        }
    }
    this.length -= 1;
};

Array.prototype.each = function(callback){
    if (!this) {
        return false;
    }
    var arr = this,len = arr.length;
    if (len > 0){
        for (var i = 0;i < len;i++){
            callback && callback.call(this,i,arr[i]);
        }
    }
};

Array.prototype.removeObjByCode = function(code) {
	if (!this || !code) {
        return false;
    } 
    var arr = this,len = arr.length,newArr = [];
    if (len > 0){
        for (var i = 0;i < len;i++){
            if (arr[i].productCode != code) {
            	newArr.push(arr[i]);
            }
        }
    }
    return newArr;
}